package j.framework.tags.easyui;

import j.framework.tags.form.FormTagWriter;
import j.util.DES;
import j.util.StringUtils;

import javax.servlet.jsp.JspException;
import java.util.Map;

/**
 * easyui 之 TreeTag
 * User: jason
 * Date: 2015/4/21 14:34
 */
public class TreeTag extends BaseEasyuiLoadRemoteDataFormControl {
    private String requestId;//从此指定的请求中获取数据
    private String selectedIds;
    private boolean checkbox;
    private boolean cascadeCheck = true;
    private boolean onlyLeafCheck;
    private String onLoadSuccess;
    private String onBeforeSelect;
    private String onSelect;
    private String onBeforeCheck;
    private String onCheck;
    @Override
    public void writeFormTagBegin(FormTagWriter writer) throws JspException {
        writer.beginTag("ul");
    }

    @Override
    public void writeFormTagProperties(FormTagWriter writer) throws JspException {

    }

    @Override
    public void writeEasyuiDataOptions(Map<String, Object> dataOptions)throws JspException{
        super.writeEasyuiDataOptions(dataOptions);
        if(checkbox){
            dataOptions.put("checkbox",true);
        }
        if(!cascadeCheck){
            dataOptions.put("cascadeCheck",false);
        }
        if(onlyLeafCheck){
            dataOptions.put("onlyLeafCheck",true);
        }
        if(StringUtils.isNotEmpty(onLoadSuccess)){
            dataOptions.put("onLoadSuccess",onLoadSuccess);
        }
        if(StringUtils.isNotEmpty(onBeforeSelect)){
            dataOptions.put("onBeforeSelect",onBeforeSelect);
        }
        if(StringUtils.isNotEmpty(onSelect)){
            dataOptions.put("onSelect",onSelect);
        }
        if(StringUtils.isNotEmpty(onBeforeCheck)){
            dataOptions.put("onBeforeCheck",onBeforeCheck);
        }
        if(StringUtils.isNotEmpty(onCheck)){
            dataOptions.put("onCheck",onCheck);
        }
        if(StringUtils.isNotEmpty(requestId)){
            String getDataUrl = String.format("/user/req?rid=%s", DES.encrypt(requestId));
            if(StringUtils.isNotEmpty(params)){
                getDataUrl+="&"+params;
            }
            dataOptions.put("url",getDataUrl);
        }
    }

    @Override
    public void endBeginTag(FormTagWriter writer) throws JspException {
        writer.writeSomething(">");
        writer.closeTag("ul");
    }

    @Override
    public String getEasyuiClassName() {
        return "easyui-tree";
    }

    @Override
    protected String getControlWidth() {
        return "100%";
    }

    public void setRequestId(String requestId) {
        this.requestId = requestId;
    }

    public void setSelectedIds(String selectedIds) {
        this.selectedIds = selectedIds;
    }

    public void setCheckbox(boolean checkbox) {
        this.checkbox = checkbox;
    }

    public void setCascadeCheck(boolean cascadeCheck) {
        this.cascadeCheck = cascadeCheck;
    }

    public void setOnlyLeafCheck(boolean onlyLeafCheck) {
        this.onlyLeafCheck = onlyLeafCheck;
    }

    public void setOnLoadSuccess(String onLoadSuccess) {
        this.onLoadSuccess = onLoadSuccess;
    }

    public void setOnBeforeSelect(String onBeforeSelect) {
        this.onBeforeSelect = onBeforeSelect;
    }

    public void setOnSelect(String onSelect) {
        this.onSelect = onSelect;
    }

    public void setOnBeforeCheck(String onBeforeCheck) {
        this.onBeforeCheck = onBeforeCheck;
    }

    public void setOnCheck(String onCheck) {
        this.onCheck = onCheck;
    }
}
